﻿using System.Collections.Generic;
using MediaLibraryManagement.DAL;
using MediaLibraryManagement.Entity;

namespace MediaLibraryManagement.BLL
{
    public class MovieBLL
    {
        private MovieDAL _dataAccess;
        private CastDAL _castDataAccess;

        public MovieBLL()
        {
            _dataAccess = new MovieDAL();
            _castDataAccess = new CastDAL();
        }

        public Movie GetMovieByID(int id)
        {
            Movie movie = (Movie)_dataAccess.GetByID(id);
            movie.Casting = _castDataAccess.GetByMovieID(id);
            return movie;
        }

        public void Insert(Movie movie)
        {
            int id = _dataAccess.Insert(movie);
            for (int i = 0; i < movie.Casting.Count; i++)
            {
                movie.Casting[i].MovieID = id;
            }
            _castDataAccess.Insert(movie.Casting);
        }

        public void Update(Movie movie)
        {
            _dataAccess.Update(movie);
            for (int i = 0; i < movie.Casting.Count; i++)
            {
                movie.Casting[i].MovieID = movie.ID;
            }
            _castDataAccess.DeleteByMovieID(movie.ID);
            _castDataAccess.Insert(movie.Casting);
        }

        public void Delete(int id)
        {
            _dataAccess.Delete(id);
            _castDataAccess.DeleteByMovieID(id);
        }

        public List<Movie> Search(string title, string genre, int year)
        {
            return _dataAccess.Search(title, genre, year);
        }
    }
}